Page({
    data: {
        loading_more_active: '',
        systemInfo: getApp().core.getSystemInfoSync(),
        placeholderHeight: 0,
        cat_id: !1,
        keyword: !1,
        page: 1,
        pageCount: 0,
        goods_list: [],
        shop_list: [],
        show_history: !0,
        show_result: !1,
        history_list: [],
        is_search: !0,
        is_show: !1,
        cats: [],
        default_cat: [],
        is_shop: false,
        latitude: 0,
        longitude: 0,
        bar_color: {
            frontColor: '#000000',
            backgroundColor: '#ffffff',
        }
    },
    onLoad: function(t) {
        var mch_id = t.mch_id ? t.mch_id : 0;
        var placeholderHeight = this.px2rpx(this.data.systemInfo.statusBarHeight) + 68
        var bar_color = getApp().core.getStorageSync(getApp().const.NAVIGATION_BAR_COLOR)
        var cat_id = t.cat_id ? t.cat_id : 0;
        console.log(cat_id)
        this.setData({
            mch_id: mch_id,
            cat_id: cat_id,
            placeholderHeight: placeholderHeight,
            bar_color: bar_color
        })
        getApp().page.onLoad(this, t), this.cats(),(cat_id&&this.getGoodsList());
        
    },
    navigatorGoods:function(e){
        getApp().core.navigateTo({
          url: '/pages/goods/goods?id=' + e.currentTarget.dataset.id
        });
    },
    px2rpx: function (px) {
        const screenWidth = this.data.systemInfo.screenWidth
        return (750 / screenWidth) * px
    },
    onReady: function(t) {
        getApp().page.onReady(this);
    },
    onShow: function(t) {
        getApp().page.onShow(this);
        this.setData({
            history_list: this.getHistoryList(!0)
        });
    },
    onReachBottom: function() {
        getApp().page.onReachBottom(this);
        var t = this, a = t.data.page + 1;
        a <= t.data.pageCount && (t.setData({
            page: a
        }), t.getGoodsList());
    },
    cats: function() {
        var a = this;
        getApp().request({
            url: getApp().api.default.cats,
            success: function(t) {
                0 == t.code && a.setData({
                    cats: t.data.list,
                    default_cat: t.data.default_cat
                });
            }
        });
    },
    change_cat: function(t) {
        var cats = this.data.cats, i = t.currentTarget.dataset.index;
        var default_cat = cats[i];
        this.setData({
            default_cat: default_cat
        });
        if (default_cat.key == 'shop') {
            wx.getLocation({
                success: (t) => {
                  this.setData({
                      longitude: t.longitude,
                      latitude: t.latitude
                  })
                }
            });
        }
    },
    pullDown: function() {
        var t = this, a = t.data.cats, e = t.data.default_cat;
        for (var s in a) a[s].id === e.id ? a[s].is_active = !0 : a[s].is_active = !1;
        t.setData({
            is_show: !t.data.is_show,
            cats: a
        });
    },
    inputFocus: function() {
        console.log(111)
        this.setData({
            show_history: !0,
            show_result: !1
        });
    },
    inputBlur: function() {
        console.log(222)
        var t = this;
        (0 < t.data.goods_list.length || 0 < t.data.shop_list.length) && setTimeout(function() {
            t.setData({
                show_history: !1,
                show_result: !0
            });
        }, 100);
    },
    inputConfirm: function(t) {
        var a = this, e = t.detail.value;
        0 != e.length && (a.setData({
            page: 1,
            keyword: e,
            goods_list: [],
            shop_list: []
        }), a.setHistory(e), a.getGoodsList());
    },
    searchCancel: function() {
        getApp().core.navigateBack({
            delta: 1
        });
    },
    historyClick: function(t) {
        var a = t.currentTarget.dataset.value;
        0 != a.length && (this.setData({
            page: 1,
            keyword: a,
            goods_list: []
        }), this.getGoodsList());
    },
    getGoodsList: function() {
        var e = this;
        if (e.data.default_cat.key == 'shop') {
            e.setData({
                is_shop: true
            });
        } else {
            e.setData({
                is_shop: false
            });
        }
        e.setData({
            show_history: !1,
            show_result: !0,
            is_search: !0
        });
        var t = {};
        // e.data.cat_id && (t.cat_id = e.data.cat_id, e.setActiveCat(t.cat_id)),
      e.data.cat_id && (t.cat_id = e.data.cat_id), e.data.keyword && (t.keyword = e.data.keyword), e.data.mch_id && (t.mch_id = e.data.mch_id), 
        t.defaultCat = JSON.stringify(e.data.default_cat), t.page = e.data.page, e.showLoadingMoreBar(), 
        t.longitude = e.data.longitude,t.latitude = e.data.latitude,
        getApp().request({
            url: getApp().api.default.search,
            data: t,
            success: function(t) {
                if (0 == t.code) {
                    if (e.data.is_shop) {
                        var a = e.data.shop_list.concat(t.data.list);
                    } else {
                        var a = e.data.goods_list.concat(t.data.list);
                    }
                    e.setData({
                        goods_list: e.data.is_shop ? e.data.goods_list : a,
                        shop_list: e.data.is_shop ? a : e.data.shop_list,
                        pageCount: t.data.page_count
                    }), 0 == t.data.list.length ? e.setData({
                        is_search: !1
                    }) : e.setData({
                        is_search: !0
                    });
                    // e.data.cat_id&&e.inputBlur()
                }
            },
            complete: function() {
                e.hideLoadingMoreBar();
            }
        });
    },
    goToUrl: function (e) {
        var id = e.currentTarget.dataset.id;
        wx.redirectTo({
          url: '/mch/shop/shop?mch_id=' + id,
        })
    },
    getHistoryList: function(t) {
        t = t || !1;
        var a = getApp().core.getStorageSync(getApp().const.SEARCH_HISTORY_LIST);
        if (!a) return [];
        if (!t) return a;
        for (var e = [], s = a.length - 1; 0 <= s; s--) e.push(a[s]);
        return e;
    },
    setHistory: function(t) {
        var a = this.getHistoryList();
        for (var e in a.push({
            keyword: t
        }), a) {
            if (a.length <= 20) break;
            a.splice(e, 1);
        }
        getApp().core.setStorageSync(getApp().const.SEARCH_HISTORY_LIST, a);
    },
    showLoadingBar: function() {
        wx.showLoading({
          title: '加载中',
        })
    },
    hideLoadingBar: function() {
        wx.hideLoading()
    },
    showLoadingMoreBar: function() {
        this.setData({
            loading_more_active: "active"
        });
    },
    hideLoadingMoreBar: function() {
        this.setData({
            loading_more_active: ""
        });
    },
    deleteSearchHistory: function() {
        this.setData({
            history_list: null
        }), getApp().core.removeStorageSync(getApp().const.SEARCH_HISTORY_LIST);
    }
});